<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Elasticsearch\ClientBuilder;
use App\Person;
use Illuminate\Support\Facades\DB;


class SearchController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->middleware('auth');
    }
    /*
     * 搜索首页
     */
    public function index()
    {
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'police',
            'type' => 'my_type',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            'match' => ['name' => '人员'],
                            //'match' => ['reg_addr' => '上海市,上海市市辖区,黄浦区'],

                        ]
                    ]
                ]
            ]
        ];

        $response = $client->search($params);
        print_r($response);

        return view('search.index');
    }

    public function import()
    {
        $client = ClientBuilder::create()->build();

        // 创建一个索引--------------
        // $params = [
        //     'index' => 'my_index',
        //     'type' => 'my_type',
        //     'id' => 'my_id',
        //     'body' => ['testField' => 'abc']
        // ];
        //
        // $response = $client->index($params);
        // print_r($response);

        // 获取一个文档--------------
        // $params = [
        //     'index' => 'my_index',
        //     'type' => 'my_type',
        //     'id' => 'my_id'
        // ];
        //
        // $response = $client->get($params);
        // print_r($response);

        // 搜索一个文档--------------
        // $params = [
        //     'index' => 'police',
        //     'type' => 'my_type',
        //     'body' => [
        //         'query' => [
        //             'bool' => [
        //                 'should' => [
        //                     'match' => ['name' => '95082'],
        //                     'match' => ['reg_addr' => '黄浦'],
        //
        //                 ]
        //             ]
        //         ]
        //     ]
        // ];

        // $response = $client->search($params);
        // print_r($response);

        // 删除一个文档--------------
        // $params = [
        //     'index' => 'my_index',
        //     'type' => 'my_type',
        //     'id' => 'my_id'
        // ];
        //
        // $response = $client->delete($params);
        // print_r($response);

        // 删除一个索引--------------
        // $deleteParams = [
        //     'index' => 'my_index'
        // ];
        // $response = $client->indices()->delete($deleteParams);
        // print_r($response);

        // 创建一个索引--------------
        // $params = [
        //     'index' => 'police',
        //     'body' => [
        //         'settings' => [
        //             'number_of_shards' => 2,
        //             'number_of_replicas' => 0
        //         ]
        //     ]
        // ];

       // $response = $client->indices()->create($params);
        //print_r($response);
        //for ($i = 0; $i < 119958; $i = $i + 1000) {
        $params = ['body' => []];
            $persons = DB::table('persons')
                ->offset(10000)
                ->limit(10000)
                ->get();
            //var_dump($persons);
            foreach ($persons as $person) {
                $params['body'][] = [
                    'index' => [
                        '_index' => 'police',
                        '_type'  => 'my_type',
                    ]
                ];

                $params['body'][] = [
                    'name'      => $person->name,
                    'id_number' => $person->name,
                    'reg_addr'  => $person->reg_addr,
                    'temp_addr' => $person->temp_addr,
                ];
            }
            $responses = $client->bulk($params);
        //}
        //var_dump($responses);
    }
}
